﻿using SqlSugar;
using Dapper;
using InventoryIssue.Infrastructrue.Interface;

namespace InventoryIssue.Infrastructrue.Implement
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly DatabaseContext db;

        public BaseRepository(DatabaseContext db)
        {
            this.db = db;
        }
        public async Task<int> DeleteAsync(T entity)
        {
            return await db.SqlSugar.Deleteable(entity).ExecuteCommandAsync();
        }

        public async Task<int> InsertAsync(T entity)
        {
            return await db.SqlSugar.Insertable(entity).ExecuteCommandAsync();
        }

        public async Task<int> UpdateAsync(T entity)
        {
            return await db.SqlSugar.Updateable(entity).ExecuteCommandAsync();
        }
        public async Task<IEnumerable<T>> QueryAsync(string sql, object parameters = null)
        {
            return await db.DapperConnection.QueryAsync<T>(sql, parameters);
        }

        public async Task<T> QueryFirstOrDefaultAsync(string sql, object parameters = null)
        {
            return await db.DapperConnection.QueryFirstOrDefaultAsync<T>(sql, parameters);
        }

        public ISugarQueryable<T> GetAll()
        {
            return db.SqlSugar.Queryable<T>();
        }
    }
}
